package cn.shyouchi.wuguanjia.android.ui.home.security.thread.message;

import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.common.Constant;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.matter.MatterNotice;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.simplenotice.SampleNotice;
import io.realm.Realm;

/**
 * Created by Eenie on 2017/3/15 at 13:31
 * Email: 472279981@qq.com
 * Des:
 */

public class MsgSectionAdapter extends BaseQuickAdapter<MsgSection, BaseViewHolder> {
    public MsgSectionAdapter(List<MsgSection> data) {
        super(R.layout.item_msg_section_layout, data);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final MsgSection msg) {
        int iconRes = mContext.getResources().getIdentifier(msg.getIconName(), "mipmap", mContext.getPackageName());
        holder.setImageResource(R.id.img_msg_icon, iconRes);
        holder.setText(R.id.tv_msg_type_name, msg.getName());

        if (holder.getPosition() == 0) {

            holder.setText(R.id.tv_msg_count, String.valueOf(matterCount()));
            if (matterCount() == 0) {
                holder.getView(R.id.tv_msg_count).setVisibility(View.INVISIBLE);
            } else {
                holder.getView(R.id.tv_msg_count).setVisibility(View.VISIBLE);
            }

//            holder.setImageResource(R.id.img_msg_icon, R.mipmap.ic_remind);

        } else if (holder.getPosition() == 1) {


            if (noticeCount() == 0) {
                holder.getView(R.id.tv_msg_count).setVisibility(View.INVISIBLE);
            } else {
                holder.getView(R.id.tv_msg_count).setVisibility(View.VISIBLE);
            }
            holder.setText(R.id.tv_msg_count, String.valueOf(noticeCount()));



//          holder.setImageResource(R.id.img_msg_icon, R.mipmap.ic_remind);

        } else {

                holder.getView(R.id.tv_msg_count).setVisibility(View.INVISIBLE);

        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.getPosition() == 2) {
                    mContext.startActivity(new Intent(mContext, msg.getTarget()).putExtra("src", Constant.MATTER_TYPE_BIRTHDAY));
                } else if (holder.getPosition() == 3) {

                    mContext.startActivity(new Intent(mContext, msg.getTarget()).putExtra("src", Constant.MATTER_TYPE_MEET_ERROR));
                } else {
                    mContext.startActivity(new Intent(mContext, msg.getTarget()));
                }
            }
        });


    }


    /**
     * 获取通知数量
     *
     * @return
     */
    private int noticeCount() {
        return (int) Realm.getDefaultInstance().where(SampleNotice.class).equalTo(SampleNotice.NOTICE_FIELD_READ, false).count();
    }


    /**
     * 获取待办事项数量
     *
     * @return
     */
    private int matterCount() {
        return (int) Realm.getDefaultInstance().where(MatterNotice.class).count();
    }

}
